#include "../../common/net.hpp"
#include "../../common/message.hpp"
#include "../../client/rpc_caller.hpp"
#include "../../client/rpc_requestor.hpp"
#include "../../client/rpc_client.hpp"
#include "../../common/muduobase/include/muduo/base/Logging.h"

void callback(const Json::Value &val)
{
    std::cout << val["ret"] << std::endl;
}

// 当前是进行服务发现的RPC客服端，需要进行服务发现
int main()
{
    // 0、先去掉内部日志。
    muduo::Logger::setLogLevel(muduo::Logger::FATAL);

    // 1、创建一个rpc客服端
    RPC::Client::RpcClient::Ptr rpc_client = std::make_shared<RPC::Client::RpcClient>(true, "127.0.0.1", 8080);

    // 2、直接向注册中心发送请求（后面如果已经有了对应的服务端直接连接即可）
    std::cout << "----------test1----------" << std::endl;
    Json::Value val1;
    val1["nums1"] = 11;
    val1["nums2"] = 22;
    Json::Value ret;
    bool sendret = rpc_client->send("Add", val1, ret);
    if (sendret == false)
    {
        ERROR_LOG("当前函数不存在对应的服务提供者");
    }
    else
    {
        std::cout << "----------msg from server----------" << std::endl;
        std::cout << ret["ret"] << std::endl;
    }

    std::cout << "----------test2----------" << std::endl;
    Json::Value val2;
    val2["nums1"] = 11;
    val2["nums2"] = 33;
    std::future<Json::Value> future;
    sendret = rpc_client->send("Add", val2, future);
    if (sendret == false)
    {
        ERROR_LOG("当前函数不存在对应的服务提供者");
    }
    else
    {
        std::cout << "----------msg from server----------" << std::endl;
        ret = future.get();
        std::cout << ret["ret"] << std::endl;
    }

    std::cout << "----------test3----------" << std::endl;
    Json::Value val3;
    val3["nums1"] = 22;
    val3["nums2"] = 33;
    sendret = rpc_client->send("Add", val3, callback);
    if (sendret == false)
    {
        ERROR_LOG("当前函数不存在对应的服务提供者");
    }
    else
    {
        std::cout << "----------msg from server----------" << std::endl;
    }
    sleep(30);
    return 0;
}